$(function () {
   vm.getInfo(getUrlParam("orderId"));
});

/**
 * 取得url参数
 */
function getUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); // 构造一个含有目标参数的正则表达式对象
    var r = window.location.search.substr(1).match(reg);  // 匹配目标参数
    if (r != null) return unescape(r[2]);
    return null; // 返回参数值
}

var vm = new Vue({
    el:'#rrapp',
    data:{
        /* 实体类 */
        orderInfo: {},
        erpOrder: {},
        orderItemList: [],
        orderItem: {},
        orderRemarks: [],
        erpOrderLogs: [],
        userBaseInfos: [],
        receiptsAccounts: [],
        userPayment: {},
        pubremark: '',
        /* 枚举 */
        e:{
            payTypeEnum: [],
            goodsTypeEnum:[],
            thirdMerchantTypeEnum:[],
            taxTypeEnum:[],
        },
        /* 查询字段 */
        q:{
            orderId: '',
            memberName: '',
            state: '',
            orderType: '',
            beginCreatetime: '',
            endCreatetime: '',
        },
        /* 订单商品审核字段 */
        audit:{
            desigener: '',
            approvalStatus: '',
            remark: '',
        },
        /* 订单支付字段 */
        pay:{
            bank: '',
            account: '',
            money: '',
            payName: '',
            memo: '',
            tPayed: ''
        },
        /* 源文件 */
        fileTableVisible: false,
        itemFile:{
            /* 订单商品集合下标标识，获得当前正在操作的商品元素 */
            itemIndex: 0,
            /* 正在操作商品的附件集合 */
            itemFiles: []
        },
    },
    methods: {
        /* 支付时间 */
        checkPayDate: function () {
            WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss',isShowClear:false, onpicking:function (dp) {
                    vm.pay.tPayed = dp.cal.getNewDateStr();
            }})
        },
        /* 获取当前日期 */
        dateFormat:function() {
            var date = new Date();
            var year=date.getFullYear();
            var month= date.getMonth()+1<10 ? "0"+(date.getMonth()+1) : date.getMonth()+1;
            var day=date.getDate()<10 ? "0"+date.getDate() : date.getDate();
            var hours=date.getHours()<10 ? "0"+date.getHours() : date.getHours();
            var minutes=date.getMinutes()<10 ? "0"+date.getMinutes() : date.getMinutes();
            var seconds=date.getSeconds()<10 ? "0"+date.getSeconds() : date.getSeconds();
            return year+"-"+month+"-"+day+" "+hours+":"+minutes+":"+seconds;
        },
        /* 银行账户 */
        receiptsAccountsChange: function (event) {
            var idx = event.target.value;
            vm.pay.bank = vm.receiptsAccounts[idx].fraBank;
            vm.pay.account = vm.receiptsAccounts[idx].fraAccount.split('-')[0];
        },
        /* 订单详情 */
        getInfo: function(orderId){
            $.get(baseURL + "order/orderinfo/mall/orderinfoaudit/" + orderId, function(r){
                /* 实体 */
                vm.erpOrder = r.erpOrder;
                vm.orderInfo = r.orderInfo;
                vm.orderItemList = r.orderItemList;
                vm.orderRemarks = r.orderRemarks;
                vm.erpOrderLogs = r.erpOrderLogs;
                vm.userBaseInfos = r.userBaseInfos;
                vm.receiptsAccounts = r.receiptsAccounts;
                vm.userPayment = r.userPayment;
                /* 枚举 */
                vm.e.payTypeEnum = r.payTypeEnum;
                vm.e.goodsTypeEnum = r.goodsTypeEnum;
                vm.e.thirdMerchantTypeEnum = r.thirdMerchantTypeEnum;
                vm.e.taxTypeEnum = r.taxTypeEnum;
            });
        },
        /* 商品审核 */
        orderItemAudit: function (idx) {
            var orderItem = vm.orderItemList[idx];
            vm.orderItem = orderItem;
            $('#remark').val('');
            layer.open({
                type: 1,
                skin: 'layui-layer-molv',
                title: "商品审核",
                area: ['600px', '500px'],
                shadeClose: false,
                content: jQuery("#orderItemLayer"),
                btn: ['保存','取消'],
                btn1: function (index) {
                    if (isBlank(vm.audit.approvalStatus)){
                        alert("请勾选审核通过按钮！");
                        return;
                    }

                    var postData = {
                        'orderId': vm.orderItem.orderId,
                        'itemId': vm.orderItem.itemId,
                        'goodsType': vm.orderItem.goodsType,
                        'desigener': vm.audit.desigener,
                        'approvalStatus': vm.audit.approvalStatus,
                        'remark': vm.audit.remark,
                    }
                    $.ajax({
                        type: "POST",
                        url: baseURL + "order/orderitem/orderItemAudit",
                        contentType: "application/json",
                        data: JSON.stringify(postData),
                        success: function(r){
                            if(r.code == 0){
                                layer.close(index);
                                alert('操作成功', function(index){
                                    vm.getInfo(vm.orderItem.orderId);
                                    vm.audit.approvalStatus = '';
                                    vm.audit.desigener = '';
                                    vm.audit.remark = '';
                                });
                            }else{
                                alert(r.msg);
                            }
                        }
                    });
                }
            });
        },
        getOrderItems: function () {
            $.get(baseURL + "order/orderitem/itemVos/"+vm.orderInfo.orderId, function(r){
                vm.orderItems = r.orderItems;
            });
        },
        getErpOrderLogs: function () {
            $.get(baseURL + "order/erporderlog/orderlist/"+vm.erpOrder.orderNo, function(r){
                vm.erpOrderLogs = r.erpOrderLogs;
            });
        },
        getOrderRemarks: function () {
            $.get(baseURL + "order/orderremarks/orderlist/"+vm.orderInfo.orderId, function(r){
                vm.orderRemarks = r.orderRemarks;
            });
        },
        orderFinish: function () {
            layer.open({
                type: 1,
                skin: 'layui-layer-molv',
                title: "订单完成",
                area: ['550px', '280px'],
                shadeClose: false,
                content: jQuery("#remarksLayer"),
                btn: ['保存','取消'],
                btn1: function (index) {
                    var remarks = $.trim($('#rRemarks').val());
                    $.ajax({
                        type: "GET",
                        url: baseURL + "order/orderinfo/mall/orderFinish",
                        contentType: "application/json",
                        data: {
                            'orderId': vm.orderInfo.orderId,
                            'remarks': vm.pubremark
                        },
                        success: function(r){
                            if(r.code == 0){
                                layer.close(index);
                                alert('操作成功', function(index){
                                    vm.getOrderRemarks();
                                    vm.getErpOrderLogs();
                                    vm.pubremark = '';
                                });
                            }else{
                                alert(r.msg);
                            }
                        }
                    });
                },
                btn2: function (index) {
                    layer.close(index);
                    vm.pubremark = '';
                }
            });
        },
        orderPay: function () {
            // 设置当前时间
            vm.pay.tPayed = vm.dateFormat();
            layer.open({
                type: 1,
                skin: 'layui-layer-molv',
                title: "订单支付",
                area: ['700px', '500px'],
                shadeClose: false,
                scrollbar: false,
                content: jQuery("#orderPayLayer"),
                btn: ['保存','取消'],
                btn1: function (index) {
                    $.ajax({
                        type: "GET",
                        url: baseURL + "order/orderinfo/mall/orderPay",
                        contentType: "application/json",
                        data: {
                            'orderId': vm.orderInfo.orderId,
                            'formdata': JSON.stringify(vm.pay)
                        },
                        success: function(r){
                            if(r.code == 0){
                                layer.close(index);
                                alert('操作成功', function(index){
                                    vm.pay.bank = '';
                                    vm.pay.money = '';
                                    vm.pay.money = '';
                                    vm.pay.payName = '';
                                    vm.pay.memo = '';
                                    vm.pay.tPayed = '';
                                    vm.getInfo(vm.orderInfo.orderId);
                                });
                            }else{
                                alert(r.msg);
                            }
                        }
                    });
                }
            });
        },
        orderInvalid: function () {
            layer.open({
                type: 1,
                skin: 'layui-layer-molv',
                title: "订单作废",
                area: ['550px', '280px'],
                shadeClose: false,
                scrollbar: false,
                content: jQuery("#remarksLayer"),
                btn: ['保存','取消'],
                btn1: function (index) {
                    var remarks = $.trim($('#rRemarks').val());
                    $.ajax({
                        type: "GET",
                        url: baseURL + "order/orderinfo/mall/orderInvalid",
                        contentType: "application/json",
                        data: {
                            'orderId': vm.orderInfo.orderId,
                            'remarks': vm.pubremark
                        },
                        success: function(r){
                            if(r.code == 0){
                                layer.close(index);
                                alert('操作成功', function(index){
                                    vm.getOrderRemarks();
                                    vm.getErpOrderLogs();
                                    vm.pubremark = '';
                                });
                            }else{
                                alert(r.msg);
                            }
                        }
                    });
                },
                btn2: function (index) {
                    layer.close(index);
                    vm.pubremark = '';
                }
            });
        },
        goBack: function () {
            window.history.go(-1);
        },
        // 展示商品文件
        itemFileShow: function (idx) {
            var itemId = vm.orderItemList[idx].itemId;
            $.ajax({
                type: "GET",
                url: baseURL + "order/useraccessory/userAccessorys/" + itemId,
                contentType: "application/json",
                data: [],
                success: function(r){
                    if(r.code === 0){
                        var goodsName = vm.orderItemList[idx].name;
                        vm.fileTableVisible = true;
                        vm.itemFile.itemIndex = idx;
                        vm.itemFile.itemFiles = r.list;
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        // 商品原文件下载
        itemSourceFileDownload: function (data) {
            window.location.href = baseURL + "order/useraccessory/download/" + data.id;
        },
        goBack: function () {
            window.history.go(-1);
        },
        /*申请售后*/
        applyService: function () {
            // 跳转申请售后页面 todo 此处应该做初步校验
            window.registerDialogVm.loadData({orderId:vm.orderInfo.orderId});
            //window.registerDialogVm.dialogRegisterVisible = true;
            //debugger;
        }
    }
});